﻿package rob.entity{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import rob.core.Primitive3d;
	import rob.core.Camera3d;
	import rob.core.World3d;
	import rob.data.Vect3d;
	import rob.maths.VectorMaths;
	import rob.maths.VertexMaths;
	
	public class Car extends Primitive3d{
		private var _anim:MovieClip;
		private var _initScale:Number;
		// direction vector, motion magnitude, _del = _dr * _dir
		private var _dir:Vect3d, _dr:Number, _del:Vect3d;
		// 3 collision points
		private var _collide1:Point, _gCollide1:Point;
		private var _collide2:Point, _gCollide2:Point;
		private var _collide3:Point, _gCollide3:Point;
		// animation
		private var _frame:Number, _frameRate:Number, _numFrames:uint;
		private var _id:uint, _visible:Boolean;
		
		public function Car(m:MovieClip):void{
			super();
			_anim = m;
			_visible = true;
			// setup collision points

			// setup position
			_initScale = m.scaleX;
			position.x =  _anim.x;
			position.z =  _anim.y;
			// direction
			_dir = new Vect3d(0,0,1);
			_dr = 1.2;
			_del = VectorMaths.scaleNew(_dr,_dir);
			
			//changeDirBy(2 * Math.PI * Math.random());
		}
		
		
		// change direction of car by angle (radians)
		public function changeDirBy(dtheta:Number):void{
			VectorMaths.rotateBy(dtheta,_dir);
			_del = VectorMaths.scaleNew(_dr,_dir);
			_anim.rotation -= dtheta * (180 / Math.PI);
		}
				
		public function update(c:Camera3d,bnds:Sprite):void{
			// move
				//VertexMaths.addTo(position,_del);
				VertexMaths.project(position,c);
				_anim.x = position.sx;
				_anim.y = position.sy;

			// check visiblity
			_visible = _anim.hitTestObject(bnds);
		}
				

		public function setDimensions(k:Number){
			_initScale *= k;
			scaleBy(1);
		}
		
		public function rotateBy(dtheta:Number):void{
			_anim.rotation -= dtheta * (180 / Math.PI);
		}
		
		public function scaleBy(scf:Number):void{
			_anim.scaleX = _anim.scaleY = _initScale * scf;
		}


		public function get gCollide1():Point{ return _gCollide1;}
		public function get gCollide2():Point{ return _gCollide2;}
		public function get gCollide3():Point{ return _gCollide3;}
		
		public function get anim():MovieClip{ return _anim;}
		public function get id():uint{ return _id;}
		public function get frame():uint{ return _frame;}
		public function get visible():Boolean{ return _visible;}

	}
}